package com.rs.game.player.content;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;

import com.rs.game.World;
import com.rs.game.player.Player;
import com.rs.game.player.content.commands.Administrator;
import com.rs.game.player.content.commands.Debug;
import com.rs.game.player.content.commands.Donator;
import com.rs.game.player.content.commands.ExtremeDonator;
import com.rs.game.player.content.commands.Moderator;
import com.rs.game.player.content.commands.NormalPlayer;
import com.rs.utils.Utils;

/**
 * Geryon Commands
 * 
 * @author Swirl <obruza@gmail.com>
 * 
 */

public final class Commands {

	/*
	 * All console commands are for admins only, chat commands are processed if
	 * console can't do it.
	 */

	/*
	 * Returns if the command was processed
	 */

	public static boolean processCommand(Player player, String command, boolean console, boolean clientCommand) {
		if (command.length() == 0) { // If they used ::(nothing) there is no command to process
			return false;
		}
		String[] cmd = command.toLowerCase().split(" ");
		if (cmd.length == 0) {
			return false;
		}

		// Process Administrator Commands
		if (player.getRights() >= 2 && player.isStaff() && Administrator.processCommand(player, cmd, console, clientCommand)) {
			return true;
		}

		// Process Moderator Commands
		if (player.getRights() >= 1 && player.isStaff() && Moderator.processCommand(player, cmd, console, clientCommand)) {
			return true;
		}

		// Process Extreme Donator Commands
		if (player.isExtremeDonator() && ExtremeDonator.processCommand(player, cmd, console, clientCommand)) {
			return true;
		}

		// Process Donator Commands
		if (player.isDonator() && Donator.processCommand(player, cmd, console, clientCommand)) {
			return true;
		}
		
		// Process Debug Commands
		if ((player.getUsername().trim().equalsIgnoreCase("Swirl")) && Debug.processCommand(player, cmd, console, clientCommand)) {
			return true;
		}

		// If nothing above, process as a normal player
		return NormalPlayer.processCommand(player, cmd, console, clientCommand);
	}
	public static boolean CanYell;
	/*
	 * Send a yell across the server
	 */
	public static void sendYell(Player player, String message, boolean staffYell) {
		if (CanYell && player.getRights() <= 1) {
			player.getPackets().sendGameMessage("<col=05f4d0> The Yell is currently disabled by an administrator!");
			return;
		}

		/*
		 * If you are muted, it won't let you yell
		 */
		if (player.getMuted() > Utils.currentTimeMillis()) {
			player.getPackets().sendGameMessage("You're temporarily muted and therefore cannot yell.");
			return;
		}

		/*
		 * Handle the staff yelling
		 */
		if (staffYell) {
			World.sendWorldMessage("[<col=ff0000>Staff Yell</col>] " + (player.getRights() > 1 ? "<img=1>" : "<img=0>") + player.getDisplayName() + ": <col=ff0000>" + message + ".</col>", true);
			return;
		}

		/*
		 * If message is over 100 characters, cut it down to 100.
		 */
		if (message.length() > 100) {
			message = message.substring(0, 100);
		}
		
		/*
		 * Logs the yells
		 */
		BufferedWriter bf;
		try {
			bf = new BufferedWriter(new FileWriter("data/logs/chatlogs/yelllog.txt", true));
			bf.write("[" + player.getDisplayName() + ", " + DateFormat.getDateTimeInstance().format(new Date()) + "]: " + message);
			bf.newLine();
			bf.flush();
			bf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		/*
		 * Handle the yelling crap
		 */
		if (player.getRights() < 2) {
			String[] invalid = { "<euro", "<img", "<img=", "<col", "<col=",
					"<shad", "<shad=", "<str>", "<u>", ".com", ".net", "no-ip" };
			/*
			 * If message contains an invalid character, it let's them know, and
			 * doesn't send the yell.
			 */
			for (String s : invalid) {
				if (message.contains(s)) {
					player.getPackets().sendGameMessage("You failed to send a yell as it had invalid characters.");
					return;
				}
			}

			/*
			 * Handle the titles for each group
			 */
			if (player.isDeveloper()) {
				World.sendWorldMessage("[<col=" + (player.getYellColor() == null ? "6666C9" : player.getYellColor()) + "><shad=000000>Developer</shad></col>]<img=10>" + player.getDisplayName() + ": <col=" + (player.getYellColor() == null ? "6666C9" : player.getYellColor()) + "><shad=000000>" + message, false);
			} else if (player.isGraphicDesigner()) {
				World.sendWorldMessage("[<col=" + (player.getYellColor() == null ? "6666FF" : player.getYellColor()) + "><shad=000000>Graphic Designer</shad></col>]<img=9>" + player.getDisplayName() + ": <col=" + (player.getYellColor() == null ? "6666FF" : player.getYellColor()) + "><shad=000000>" + message, false);
			} else if (player.isStaff()) {
				World.sendWorldMessage("[<col=" + (player.getYellColor() == null ? "008B00" : player.getYellColor()) + "><shad=000000>Moderator</shad></col>] <img=0>" + player.getDisplayName() + ": <col=" + (player.getYellColor() == null ? "008B00" : player.getYellColor()) + "><shad=000000>" + message, false);
			} else if (player.isExtremeDonator()) {
				World.sendWorldMessage("[<col=" + (player.getYellColor() == null ? "02B41D" : player.getYellColor()) + "><shad=000000>Extreme Donator</shad></col>] <img=4>" + player.getDisplayName() + ": <col=" + (player.getYellColor() == null ? "02B41D" : player.getYellColor()) + "><shad=000000>" + message, false);
			} else if (player.isDonator()) {
				World.sendWorldMessage("[<col=FF0000><shad=000000>Donator</shad></col>] <img=5>" + player.getDisplayName() + ": <col=FF0000><shad=000000>" + message, false);
			} else {
				World.sendWorldMessage("[<col=9B30FF><shad=000000>Player</shad></col>] " + player.getDisplayName() + ": <col=9B30FF><shad=000000>" + message, false);
			}
		} else if (player.getUsername().equalsIgnoreCase("Kyle")) {
			World.sendWorldMessage("[<col=" + (player.getYellColor() == null ? "02b41d" : player.getYellColor()) + "><shad=000000>Veteran</shad></col>] <img=4>" + player.getDisplayName() + ": <col=" + (player.getYellColor() == null ? "03b41d" : player.getYellColor()) + "><shad=000000>" + message, false);

		} else if (player.getUsername().equalsIgnoreCase("Swirl")) {
			World.sendWorldMessage("[<col=" + (player.getYellColor() == null ? "FF8000" : player.getYellColor()) + "><shad=000000>Owner</shad></col>] <img=1>" + player.getDisplayName() + ": <col=" + (player.getYellColor() == null ? "FF8000" : player.getYellColor()) + "><shad=000000>" + message, false);
		} else {
			World.sendWorldMessage("[<col=" + (player.getYellColor() == null ? "336699" : player.getYellColor()) + "><shad=000000>Admin</shad></col>] <img=1>" + player.getDisplayName() + ": <col=" + (player.getYellColor() == null ? "336699" : player.getYellColor()) + "><shad=000000>" + message, false);
		}
	}

	/*
	 * Doesn't let it be instanced
	 */
	private Commands() {
	}
}